﻿using DAL;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Transactions;

namespace BLL
{
    public class B_Admin
    {
        private DAL.D_Admin users = new DAL.D_Admin();
        DAL.D_Admin dal = new DAL.D_Admin();

        //管理员端登录
        public int Login(Models.Admin_1 user)
        {
            var res = dal.ValidateUser(user);
            return res ? 0 : 1;
        }

        //导入管理员数据
        public bool Add_A(List<Admin_1> list)
        {
            try
            {
                return dal.add_A(list);
            }
            catch
            {
                throw new Tools.Add_Exception("添加失败！");
            }
        }

        //删除管理员数据
        public bool Del_A(List<Admin_1> list)
        {
            try
            {
                return dal.del_A(list);
            }
            catch
            {
                throw new Tools.Del_Exception("删除失败！");
            }
        }

        //保存管理员数据修改
        public bool Save_List_A(List<Admin_1> admins)
        {
            try
            {
                return dal.save_List_A(admins);
            }
            catch
            {
                throw new Tools.Modify_Exception("修改失败！");
            }
        }

        //导入管理员数据页面的换页功能
        public List<Admin_1> GetPage_A(int Page_idx, int Page_size, ref int totalCount)
        {
            return dal.getPage_A(Page_idx, Page_size, ref totalCount);
        }

        //查询学生数据
        public List<Student> GetList_S()
        {
            return dal.getList_S();
        }

        //导入学生数据
        public bool Add_S(List<Student> list)
        {
            try
            {
                return dal.add_S(list);
            }
            catch
            {
                throw new Tools.Add_Exception("添加失败！");
            }
        }

        //删除学生数据
        public bool Del_S(List<Student> list)
        {
            try
            {
                return dal.del_S(list);
            }
            catch
            {
                throw new Tools.Del_Exception("删除失败！");
            }
        }

        //保存学生数据修改
        public bool Save_List_S(List<Student> students)
        {
            try
            {
                return dal.save_List_S(students);
            }
            catch
            {
                throw new Tools.Modify_Exception("修改失败！");
            }
        }

        //导入学生数据页面的换页功能
        public List<Student> GetPage_S(int Page_idx, int Page_size, ref int totalCount)
        {
            return dal.getPage_S(Page_idx, Page_size, ref totalCount);
        }

        //导入老师数据
        public bool Add_T(List<Teacher> list)
        {
            try
            {
                return dal.add_T(list);
            }
            catch
            {
                throw new Tools.Add_Exception("添加失败！");
            }
        }

        //删除老师数据
        public bool Del_T(List<Teacher> list)
        {
            try
            {
                return dal.del_T(list);
            }
            catch
            {
                throw new Tools.Del_Exception("删除失败！");
            }
        }

        //保存老师数据修改
        public bool Save_List_T(List<Teacher> teachers)
        {
            try
            {
                return dal.save_List_T(teachers);
            }
            catch
            {
                throw new Tools.Modify_Exception("修改失败！");
            }
        }

        //查询老师数据
        public List<Teacher> GetList_T()
        {
            return dal.getList_T();
        }

        //导入老师数据页面的换页功能
        public List<Teacher> GetPage_T(int Page_idx, int Page_size, ref int totalCount)
        {
            return dal.getPage_T(Page_idx, Page_size, ref totalCount);
        }

        //查询学生组信息
        public List<StuGroup> GetList_G()
        {
            return dal.getList_G();
        }

        //保存学生组信息
        public bool Save_List_G(List<StuGroup> groups)
        {
            return dal.save_List_G(groups);
        }

        //按老师ID排序查询学生组信息
        public List<StuGroup> GetList_S_T()
        {
            return dal.getList_S_T();
        }

        //学生表和学生组表连表查询
        public List<Student> GetList_S_G(Models.StuGroup group)
        {
            return dal.getList_S_G(group);
        }

        //学生组自动分配结果保存
        public bool Save_result_S(List<Models.StuChoice> gs)
        {
            try
            {
                return dal.save_result_S(gs);
            }
            catch
            {
                throw new Tools.Modify_Exception("保存失败！");
            }
        }

        //学生组选择结果查询
        public List<Models.StuChoice> GetList_S_C()
        {
            return dal.getList_S_C();
        }

        //老师自动分配结果保存
        public bool Save_result_T(List<Models.TeaChoice> ts)
        {
            try
            {
                return dal.save_result_T(ts);
            }
            catch
            {
                throw new Tools.Modify_Exception("保存失败！");
            }
        }

        //老师选择结果查询
        public List<Models.TeaChoice> GetList_T_C()
        {
            return dal.getList_T_C();
        }

        //管理员端发布结果
        public bool Save_result_A(List<Models.TeaChoice> ts, List<Models.StuChoice> gs)
        {
            B_Admin b_Admin = new B_Admin();
            BLL.B_Student b_Student = new BLL.B_Student();
            BLL.B_Group b_Group = new BLL.B_Group();
            BLL.B_Teacher b_Teacher = new BLL.B_Teacher();

            bool res;

            using (TransactionScope tx = new TransactionScope())//事务处理
            {
                var res_g = b_Admin.Save_result_S(gs);
                var res_t = b_Admin.Save_result_T(ts);

                var students = b_Student.GetList();
                var all_groups = b_Group.GetList();
                var all_teachers = b_Teacher.GetList();

                foreach (StuChoice StuChoice in gs)
                {
                    students.Where(stu => stu.group_id == StuChoice.Group_id).ToList().ForEach(stu => stu.group_id = StuChoice.Group_id);
                    all_groups.Where(group => group.group_id == StuChoice.Group_id).ToList().ForEach(group => group.teacher_id = StuChoice.T_id_first);
                }

                foreach (Teacher t_data in all_teachers)
                {
                    all_groups.Where(group => group.teacher_id == t_data.teacher_id).ToList().ForEach(group => group.teacher_name = t_data.teacher_name);
                }

                var res_stu = b_Student.SaveList(students);
                var res_groups = b_Group.SaveList(all_groups);

                if (res_g && res_t && res_stu && res_groups)
                {
                    tx.Complete();
                    res = true;
                }
                else
                {
                    tx.Dispose();
                    res = false;
                }
            }
            return res;
        }
    }

}
